<template>
    <div>
        <div class="e-form">
            <BillTop @cancel="handleClose"></BillTop>
            <div class="tabs" style="margin-bottom:120px">
                <div class="tabs-title">基本信息</div>
                <div style="width: 100%" class="form">
                    <el-form
                        :model="formData"
                        :label-width="formLabelWidth"
                        :rules="rulesBase"
                        ref="rulesBase"
                        class="demo-ruleForm"
                    >
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="合同编号：">
                                        <span>{{ formData.contractNumber }}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更编号：">
                                        <span>{{ formData.changeApplyChangeNumber }}</span>
                                        <el-button class="btn-blue" style="margin-left:5px" @click="jumpContractDefinitionShow">查看</el-button>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="合同名称：">
                                    <span>{{formData.contractName}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="所属机构：">
                                    <span>{{ formData.orgName }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="是否需要缴纳履约保证金：" prop="bondAmount">
                                    <el-switch
                                        v-model="formData.bondState"
                                        active-color="#06ad36"
                                        inactive-color="#C0CCDA"
                                        :active-value="true"
                                        :inactive-value="false"
                                        active-text="是"
                                        inactive-text="否"
                                    >
                                    </el-switch>
                                    <el-input
                                        v-model="
                                            formData.bondAmount
                                        "
                                        v-enter-float="{
                                            set: this,
                                            val: 'formData',
                                            key: 'bondAmount'
                                        }"
                                        autocomplete="off"
                                        v-if="formData.bondState"
                                        style="margin-left:40px"
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="不含税金额(元)：">
                                    <span>{{
                                        formData.taxExcludedAmount
                                    }}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="服务单位费用(元)：">
                                    <span>{{formData.serverUnitAmount}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item
                                    label="原预付款(元)："
                                    prop="advanceCharge"
                                >
                                    <span>{{
                                        formData.advanceCharge
                                    }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="原合同金额(元)：">
                                    <span>{{
                                        formData.contractAmount
                                    }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="变更后合同金额：">
                                    <span>{{formData.contractChangeAmount}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="变更期数：">
                                    <span>{{ formData.period }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注：">
                                    <el-input type="textarea" v-model="formData.contractContext" autocomplete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="传输财务共享：">
                                    <span class="redFont" v-if="formData.financialSharing === 0 || formData.financialSharing === 1 || formData.financialSharing === 2 || formData.financialSharing === 4">{{ financialSharingList[formData.financialSharing] }}</span>
                                    <span class="greenFont" v-if="formData.financialSharing === 3 || formData.financialSharing === 5">{{ financialSharingList[formData.financialSharing] }}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="变更日期：">
                                    <span>{{formData.gmtCreate}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="币种：">
                                    <span>{{formData.currencyName}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="经办人：">
                                    <span>{{userInfo.userName}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="本位币：">
                                    <span>{{formData.baseCurName}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态：">
                                    <span>{{state.find(x=>x.value === formData.state).label}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-col :span="12">
                                    <el-form-item label="本位币汇率：" prop="baseCurRate">
                                        <span>{{formData.baseCurRate}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="人民币汇率：">
                                    <span>{{formData.rmbRate}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </div>
            <div class="buttons">
                <el-button type="primary" size="small" style="background: #2e61d7" @click="addSave"
                    >保存</el-button
                >
                <el-button size="small" @click="handleClose">取消</el-button>
            </div>
        </div>
    </div>
</template>
<script>
import { mapState } from 'vuex'
import ctChangesMaterialApply from '@/api/balanceContractApi/ctMaterialPurchaseApi/ctChangesMaterialApply.js'
import ctSupplementSigning from '@/api/balanceContractApi/ctMaterialPurchaseApi/ctSupplementSigning.js'
import { formatDate, showLoading, hideLoading, selectDataClient } from '@/utils/common'
import { showContract } from '@/pages/subcontract/common/contractCom.js'
export default {
    data () {
        return {
            //基础信息
            formData: {
                'address': '',
                'advanceCharge': 0,
                'advanceChargeChange': 0,
                'baseCurId': '',
                'baseCurName': '',
                'baseCurRate': 0,
                'billId': '',
                'bondAmount': 0,
                'bondState': true,
                'changeAmount': 0,
                'changeApplyBillId': '',
                'changeApplyChangeNumber': '',
                'changeTime': '',
                'contractAmount': 0,
                'contractChangeAmount': 0,
                'contractContext': '',
                'contractName': '',
                'contractNumber': '',
                'contractState': 0,
                'contractType': '',
                'contractTypeId': '',
                'currencyId': '0',
                'currencyName': '',
                'financialSharing': 0,
                'founderId': '',
                'founderName': '',
                'gmtCreate': '',
                'gmtModified': '',
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDescription': '',
                'nullifyReason': '',
                'orgId': '',
                'orgName': '',
                'originalBillId': '',
                'period': 0,
                'rmbName': '',
                'rmbRate': 0,
                'serverTaxRate': 0,
                'serverUnitAmount': 0,
                'signingDate': '',
                'signingEndTime': '',
                'signingId': '',
                'signingName': '',
                'signingStartTime': '',
                'state': 0,
                'taxAmount': 0,
                'taxExcludedAmount': 0,
                'taxRate': 0,
                'workId': ''
            },
            formLabelWidth: '200px',
            ctAddressCode: [],
            billid: this.$route.query.billid,
            rulesBase: {
                taxAmount: [
                    { required: true, message: '请输入税额', trigger: 'blur' },
                ],
                taxRate: [
                    { required: true, message: '请输入0-100的数字', trigger: 'blur' },
                ],
                advanceChargeChange: [
                    { required: true, message: '请输入变更后预付款', trigger: 'blur' },
                ],
                bondState: [
                    {
                        required: true,
                        message: '请选择是否需要缴纳履约保证金：',
                        trigger: 'change'
                    }
                ],
                bondAmount: [
                    {
                        required: false,
                        validator: (rule, value, callback)=>{
                            console.log('value', value)
                            if(this.formData.bondState) {
                                if(!this.formData.bondAmount || this.formData.bondAmount <= 0) {
                                    callback('应交履约保证金必须大于0')
                                }else{
                                    callback()
                                }
                            }else{
                                callback()
                            }

                        },
                        trigger: 'blur'
                    }
                ],
            },
            disabled: false,
            bondState: false, //原合同是否应交履约保证金
        }
    },
    mounted () {
        this.getBaseInfo()
    },
    components: {
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify,
            userInfo: state => state.userInfo,
            financialSharingList: state => state.contract.ctClassify.financialSharingList, //传输财务共享
            state: state => state.equip.equipData.state, //通用流程状态
        })
    },
    watch: {
        //监听是否应交保证金切换
        'formData.bondState': {
            handler (val) {

                this.rulesBase.bondAmount[0].required = val
                if(val === false) {
                    this.formData.bondAmount = 0
                }
                if(this.$refs.rulesBase) {
                    let dome =  this.$refs.rulesBase.fields.find(x=>x._props.prop === 'bondAmount')
                    if(dome) {
                        dome.onFieldBlur()
                    }
                }
            },
            immediate: true
        },
    },
    methods: {
        handleBondState (val) {
            this.disabled = val ? false : true
        },
        //默认获取基础信息
        getBaseInfo () {
            ctChangesMaterialApply.getInfo(this.billid).then(res=>{
                for(let key in res) {
                    this.formData[key] = res[key]
                }
                this.bondState = res.bondState
                this.formData.originalBillId = this.$route.query.originalBillId
                this.formData.changeApplyBillId = this.$route.query.billid
                this.formData.changeApplyChangeNumber = res.changeNumber
                this.formData.advanceCharge = this.formData.advanceChargeChange
                this.formData.contractAmount = this.formData.contractChangeAmount
                this.formData.state = 0
                this.formData.billId = ''
                this.formData.changeTime = formatDate(new Date(), 'yyyy-MM-dd')
                this.formData.signingDate = formatDate(new Date(), 'yyyy-MM-dd')
            })
        },
        //基本信息保存
        addSave () {
            this.$refs.rulesBase.validate(valid => {
                if (valid) {
                    showLoading()
                    ctSupplementSigning.createInfo(this.formData).then(res=>{
                        hideLoading()
                        this.clientPop('info', '保存成功，是否继续？', ()=>{
                            this.$router.replace({
                                path: '/ctSupplementSigningEditHV',
                                query: {
                                    billid: res,
                                    name: 'mainInfo',
                                    originalBillId: this.$route.query.originalBillId
                                }
                            })
                        }, null, this.handleClose)
                    })
                } else {
                    return false
                }
            })
        },
        //取消
        handleClose () {
            hideLoading()
            this.$router.replace('/ctMaterialPurchaseHV')
        },
        //跳转到合同申请查看页面
        jumpContractDefinitionShow () {
            showContract('ctChangesMaterialApplyShowHV', this.$route.query.billid)
        },
        //获取签约人
        getSigningName () {
            selectDataClient({
                SelectServiceToken: '/hr/user/getUserUnderOrgPage', // 客户端对应方法名
                IsSingleMode: true, // 是否单选 true 单选 false 多选
                SelectedIDs: [], // 已选中id
                InitParamsStr: '', //初始化参数
                Title: '选择人员' // 标题
            }).then(val => {
                this.formData.signingId = val[0].userId
                this.formData.signingName = val[0].userName
            })

        },
    },
}

</script>

<style lang='scss' scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
.e-table{
    min-height: auto;
    background: #fff;
}
.separ{
   width: 30px;
   height: 40px;
   line-height: 18px;
   text-align: center;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}

</style>